/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.stats;

import java.util.*;

/*******************************************************************************
 * This thread monitors nfse.DataSockets and periodically calls
 * nfse.DataSocket.purgeRecords() to avoid starvation of low-volume data
 * streams.
 * 
 */
public class StatsSocketMonitor extends Thread {

    /***************************************************************************
     * The interval to call DataSocket.purgeRecords().
     */
    int seconds = 300;

    /***************************************************************************
     * The DataSockets instantiated by the nfse.DataServer associated with this
     * thread.
     */
    Vector<StatsSocket> statsSockets = new Vector<StatsSocket>();

    /***************************************************************************
     * Basic constructor sets the seconds attribute.
     * 
     * @param seconds
     *            Determines how often to check the nfse.DataSocket's record
     *            buffer.
     * 
     */
    public StatsSocketMonitor(int seconds) {
        this.seconds = seconds;
    }

    /***************************************************************************
     * Adds a nfse.DataSocket to monitor. Sockets are removed by chechSockets()
     * when they are found to be closed/inactive.
     * 
     * @param ds
     *            The nfse.DataSocket to add.
     */
    public synchronized void addStatsSocket(StatsSocket ds) {
        statsSockets.addElement(ds);
    }

    /***************************************************************************
     * For each nfse.DataSocket being monitored, check to see if it is still
     * active. If not, remove it from the list. Else call purgeRecords().
     */
    public synchronized void checkSockets() {
        for (int i = 0; i < statsSockets.size(); i++) {
            StatsSocket ds = (StatsSocket) statsSockets.elementAt(i);
            if (!ds.isAlive()) {
                statsSockets.removeElementAt(i);
                i--;
            } else {
                try {
                    ds.rotateFile();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /***************************************************************************
     * Runs forever, periodically calling purgeRecords() on nfse.DataSockets in
     * the dataSockets Vector.
     */
    public void run() {

        while (true) {
            try {

                // Record the start time
                long start = System.currentTimeMillis();

                checkSockets();

                // Record the end time
                long end = System.currentTimeMillis();

                // Sleep for any amount of time remaining
                if (end - start < seconds * 1000) {
                    Thread.sleep(seconds * 1000 * (end - start + 1));
                } else {
                    System.out
                            .println("The checkSockets() call took longer than "
                                    + seconds
                                    + " seconds to execute. Consider increasing the seconds attribute, lowering the load to the server or upgrading to better hardware.");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

}